import {defineStore} from "pinia";
import {ref} from 'vue';
import {
    useModifyOneAPI,
    useSaveOneMaterialInAPI,
    useGetAllMaterialInAPI,
    useGetAllMaterialInCountsAPI,
    useGetAllByMultifunctionNameAPI,
    useGetAllCountsByMultifunctionNameAPI, useGetByBetweenTimesAPI, useGetAllCountsByBetweenTimesAPI,
    useDeleteOneByIdAPI, useSaveOneMaterialInFormAPI, useGetOneByIdAPI
} from '@/apis/materialIn.ts';
import {RouteParamValue} from "vue-router";


export const useMaterialInStore=defineStore('materialIn',()=>{

//     材料进场表单列表数据state
    const allMaterialInList=ref([]);
    const allMaterialInCounts=ref(0);
    const materialInStore=ref({});
    const result=ref({});


//     分页获取所有材料进场表单列表数据，包含同时获取所有数据行条数
    const getAllMaterialInStore=async (current:number,pageSize:number)=>{
        const res=await useGetAllMaterialInAPI(current,pageSize);
        allMaterialInList.value=res.data;
        allMaterialInCounts.value=(await useGetAllMaterialInCountsAPI()).data;
    }

    // 新增一条材料进场信息
    const saveOneMaterialInStore=async (formData:FormData)=>{
        await useSaveOneMaterialInAPI(formData);
    }
    // 修改一条材料进场信息
    const modifyOneMaterialStore=async (formData:FormData)=>{
      result.value= await useModifyOneAPI(formData);
    }
    // 多功能名称查询接口，同时也查询所有符合名称要求的数据条数

    const getAllByMultifunctionNameStore=async (name:string,current:number,pageSize:number)=>{
        allMaterialInList.value=(await useGetAllByMultifunctionNameAPI(name,current,pageSize)).data;
        allMaterialInCounts.value=(await useGetAllCountsByMultifunctionNameAPI(name,current,pageSize)).data;
    }

    //  根据时间段查询数据,同时也查询到所有符合要求得数据条数
    const getByBetweenTimesStore=async (start:Date,end:Date, current:number,pageSize:number)=>{
      allMaterialInList.value=(await useGetByBetweenTimesAPI(start,end,current,pageSize)).data;
      allMaterialInCounts.value=(await useGetAllCountsByBetweenTimesAPI(start,end)).data;
    }

    // 根据id删除一条数据
    const deleteOneById=async (id:number)=>{
        await useDeleteOneByIdAPI(id)
    }

    // 添加一个材料进场数据
    const saveOneMaterialInForm= async (formData:FormData)=>{
        await useSaveOneMaterialInFormAPI(formData);
    }

    // 根据id 获取一条MaterialIn数据
    const getOneByIdStore =async (id: string | RouteParamValue[])=>{
      const res=  await useGetOneByIdAPI(id);
      materialInStore.value=res.data;
    }




    return {
        allMaterialInList,allMaterialInCounts,
        getAllMaterialInStore,saveOneMaterialInStore,
        modifyOneMaterial: modifyOneMaterialStore,
        getAllByMultifunctionNameStore,getByBetweenTimesStore,
        deleteOneById,saveOneMaterialInForm,materialInStore,getOneByIdStore,
        result
    }
})